package movieGandalf.dbParser;

import java.io.BufferedReader;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;

import movieGandalf.database.MovieTools;

public class ActorsAdder implements DataReader{
	
	public int read(Connection conn, BufferedReader in){
		String line = null;
		int len = 0;
		int stringEnd = 0;
		long count = 0;
		
		PreparedStatement stmt = null;
		
		HashMap<String,BigInteger> tempActors = null;
		
		String firstChar = "";
		
		String imdbName = null;
		BigInteger movieID = null;
		String actorName = null;
		BigInteger actorID = null;
		String role = null;
		
		try {
			boolean isBegin = true;
			
			stmt = conn.prepareStatement("REPLACE INTO MovieActors (movieID,actorID,role) VALUES(?,?,?)");
			
			while ((line = in.readLine()) != null){
				
				if (isBegin){
					line=line.trim();
					len=line.length();
					
					if (len<6 || !line.subSequence(0, 4).equals("Name") || !line.subSequence(len-6, len).equals("Titles"))
						continue;
					else{
						isBegin = false;
						line = in.readLine();
						continue;
					}
				}
				
				len=line.length();
				
				if (len>=5 && line.startsWith("-----"))
					break;
				
				int i = 0;
				for (i=0;i<len;i++){
					if (line.charAt(i)=='\t') {
						break;
					}
				}
				
				if (i > 0){
					actorName = line.substring(0, i).trim();
					if (!MovieTools.actorValidator(actorName))
						continue;
					
					if (actorName.substring(0,1).compareToIgnoreCase(firstChar)!=0){
						firstChar = actorName.substring(0,1);
						tempActors = MovieTools.getActors(conn, firstChar);
					}
					
					actorID = tempActors.get(actorName);
				}
				
				if (actorID == null)
					continue;
				
				line = line.substring(i,len).trim();
				len = line.length();
				
				if (len==0 || line.charAt(0)=='"')
					continue;
				
				for (i=len-1;i>=0 && line.charAt(i)!=']';i--);
				stringEnd = i;
				for (;i>=0 && line.charAt(i)!='[';i--);
				
				if (i <= 0){
					role = null;
					line = line + "\t";
				}
				else{
					role = line.substring(i+1,stringEnd);
					line = line.substring(0,i-1)+"\t";
				}
				
				imdbName = ParserTools.getIMDBName(line);
				if (!MovieTools.movieValidator(imdbName))
					continue;
				
				movieID = MovieTools.getMovieID(conn, imdbName);
				if (movieID == null)
					continue;
				
				stmt.setObject(1, movieID);
				stmt.setObject(2, actorID);
				stmt.setObject(3, role);
				stmt.addBatch();
				count++;
				
				if (count%1000000==0){
					try{
						stmt.executeBatch();
					}
					catch(Exception e){}
					finally{
						conn.commit();
						stmt.close();
						stmt = conn.prepareStatement("REPLACE INTO MovieActors (movieID,actorID,role) VALUES(?,?,?)");
					}
				}
			}
			if (count%1000000!=0){
				try{
					stmt.executeBatch();
				}
				catch(Exception e){}
				finally{
					conn.commit();
					stmt.close();
				}
			}
		}
		
		catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		catch (Exception e){
			e.printStackTrace();
		}
		finally {
			try{
				in.close();
			}
			catch(Exception e){
				return 0;
			}
		}
		
		return 1;
	}
	
}
